package com.itheima.springbootrequestresponse.controller;

import com.itheima.springbootrequestresponse.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * RequestController
 *
 * @author liliudong
 * @version 1.0
 * @description 映射都没有 哪来的接口呢 扫描到 经验导致（风格都不一样）
 * @date 2023/7/7 10:14
 */
@RestController
public class RequestController {

    /**
     * 原始方式（不推荐）
     * 类型需要二次转换
     * 用法繁琐
     *
     * @param request
     * @return
     */
    @RequestMapping("/simpleParam1")
    public String simpleParam1(HttpServletRequest request) {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        int ageInt = Integer.parseInt(age);
        System.out.println("参数是：name = " + name + "age = " + ageInt);
        return "简单参数1";
    }

    /**
     * 简单参数2（推荐）
     * KEY 前后端传KEY的名字和类型要完全对应
     *
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/simpleParam2")
    public String simpleParam2(String name, Integer age) {
        System.out.println("参数是：name = " + name + "age = " + age);
        return "简单参数2";
    }

    /**
     * 简单参数
     * 1.别名
     * 2.必须的
     * 3.设置默认值
     *
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/simpleParam3")
    public String simpleParam3(@RequestParam(name = "name1") String name,
                               @RequestParam(name = "userAge") Integer age) {
        System.out.println("参数是：name = " + name + " age = " + age);
        return "简单参数2";
    }

    /**
     * 实体参数
     * 1. 封装
     *
     * @param user
     * @return
     */
    @RequestMapping("/pojoParam1")
    public String pojoParam(User user) {
        System.out.println("实体参数：" + user);
        return "实体参数";
    }

    /**
     * 实体参数 - 嵌套（不常用）
     * 1. 属性名.属性名
     *
     * @param user
     * @return
     */
    @RequestMapping("/pojoParam2")
    public String pojoParam2(User user) {
        System.out.println("实体参数：" + user);
        return "pojoParam2";
    }

    /**
     * 数组参数
     *
     * @param hobby
     * @return
     */
    @RequestMapping("/arrParam")
    public String pojoParam2(String[] hobby) {
        System.out.println(Arrays.toString(hobby));
        return "arrParam";
    }

    /**
     * 集合参数
     * 集合参数必须使用@RequestParam
     *
     * @param hobby
     * @return
     */
    @RequestMapping("/listParam")
    public String listParam(@RequestParam List<String> hobby) {
        System.out.println(hobby);
        return "listParam";
    }

    /**
     * 日期参数
     *
     * @param brithday
     * @return
     */
    @RequestMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime brithday) {
        System.out.println(brithday);
        return "dateParam";
    }

    /**
     * JSON参数（推荐的）
     * 1. 不限制大小
     *
     * @param user
     * @return
     */
    @RequestMapping("/jsonParam")
    public String jsonParam(@RequestBody User user) {
        System.out.println(user);
        return "jsonParam";
    }

    /**
     * 路径参数 ?id2=xx
     *
     * @param id
     * @return
     */
    @RequestMapping("/pathParam/{id}/{id2}")
    public String pathParam(@PathVariable String id, @PathVariable Long id2) {
        System.out.println(id + " |||| " + id2);
        return "pathParam";
    }
}
